package zoo.cart.admin.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import zoo.cart.admin.dao.CartDao;
import zoo.cart.admin.domain.CartCommand;
import zoo.member.admin.util.PagingUtil;

@Controller
public class CartListController {
	
	private int pageSize = 10;
	private int blockCount = 10;
	private String pageUrl = "cart.do";
	
	@Autowired
	private CartDao cartDao;
	
	@ModelAttribute("cartCommand")
	public CartCommand formBacking(){
		return new CartCommand();
	}
	
	@RequestMapping("/admin/cart.do")
	public ModelAndView process(@RequestParam(value="currentPage",defaultValue="1") int currentPage,
			@RequestParam(value="keyField",defaultValue="0")String keyField,
			@RequestParam(value="keyWord",defaultValue="0")String keyWord){
		String pagingHtml; //페이징을 구현한 HTML
		
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("keyField", keyField);
		map.put("keyWord", keyWord);
		
		//총 글의 갯수 검색된 글의 갯수
		int count = cartDao.getCartRowCount(map);
		
		PagingUtil page =new PagingUtil(pageUrl,keyField,keyWord,currentPage,count,blockCount,pageSize);
		
		pagingHtml = page.getPagingHtml().toString();
		
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		
		List<CartCommand> list = null;
		if(count>0){
			list = cartDao.cartlist(map);
		}else{
			list = Collections.emptyList(); 
		}
		//글 목록에 표시할 연번
		int number = count-(currentPage-1)*pageSize; 
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin_order_list");
		mav.addObject("count",count);
		mav.addObject("list",list);
		mav.addObject("pagingHtml",pagingHtml);
		mav.addObject("number",number);
			
		return mav;
	}
	
}
